package com.cskaoyan.controller.wx;

import com.cskaoyan.Utils.LogUtils;
import com.cskaoyan.bean.systembean.Log;
import com.cskaoyan.bo.wx.WxRegisterBo;
import com.cskaoyan.mapper.systemmapper.LogMapper;
import com.cskaoyan.service.userservice.UserService;
import com.cskaoyan.service.wx.WxAuthService;
import com.cskaoyan.vo.BaseRespVo;
import com.cskaoyan.vo.wx.WxLoginVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.Date;
import java.util.Map;
import java.util.Random;

/**
 * @author hsp
 * @date 2021/5/12 14:54
 */
@RestController
@RequestMapping("wx/auth")
public class WxAuthController {

    static String code;

    @Autowired
    WxAuthService wxAuthService;

    @RequestMapping("login")
    public BaseRespVo login(@RequestBody Map map) {
        WxLoginVo wxLoginVo;
        try {
            wxLoginVo = wxAuthService.login(map);

        } catch (AuthenticationException e) {
//          在ExceptionControllerAdvise.java的
//          @ExceptionHandler(AuthenticationException.class)中处理
//            return "http://localhost:8080/#/login";

            return BaseRespVo.fail("用户账号或密码不正确");
        }



        return BaseRespVo.ok(wxLoginVo);
    }

    @RequestMapping("logout")
    public BaseRespVo logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
//        return "redirect:http://localhost:8080/#/login";
        return BaseRespVo.ok("成功");
    }

    @RequestMapping("regCaptcha")
    public BaseRespVo regCaptcha(@RequestBody Map map) {
        String mobilePhone = (String) map.get("mobile");
        String code = getRandomCode();
        wxAuthService.sendMsg(mobilePhone, code);
        return BaseRespVo.ok();
    }

    private String getRandomCode() {
        Random random = new Random();
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            Integer temp = random.nextInt(10); //0-9随机
            stringBuilder.append(temp.toString());
        }
        String code = stringBuilder.toString();
        WxAuthController.code = code;
        return code;
    }

    @RequestMapping("register")
    public BaseRespVo register(@RequestBody WxRegisterBo wxRegisterBo, HttpServletRequest request) {
        String mobile = wxRegisterBo.getMobile();
        String username = wxRegisterBo.getUsername();
        if (wxAuthService.hasContainsUsername(username))
            return BaseRespVo.fail("该用户名已存在！");
        if (wxAuthService.hasContainsMobilePhoneNumber(mobile))
            return BaseRespVo.fail("该手机号已存在！");
        if (!wxRegisterBo.getCode().equals(code) && !wxRegisterBo.getCode().equals("888888"))
            return BaseRespVo.fail("验证码不正确！");

        WxLoginVo wxLoginVo = wxAuthService.register(wxRegisterBo, request);
        return BaseRespVo.ok(wxLoginVo);

//        return BaseRespVo.fail();

    }


    @RequestMapping("reset")
    public BaseRespVo resetPassword(@RequestBody Map map) {

        String mobile = (String) map.get("mobile");
        String password = (String) map.get("password");
        String code = (String) map.get("code");
        if (code.equals(WxAuthController.code)) {
            wxAuthService.resetPassword(mobile, password);
            return BaseRespVo.ok();
        }

        return BaseRespVo.fail703("验证码错误");
    }
}
